package action;

/**
 * Action que gera um valor que varia entre -Amplitude e +Amplitude no dado periodo.
 * Use a propiedade Periodic Mode para definir a forma do cauculo.
 * @author Ygor
 *
 */
public class PeriodicAction extends BaseAction<Float> {

	public enum PeriodicMode {
		Sine, Cossine, Linear
	}
	PeriodicMode periodicMode;
	
	public PeriodicAction(Float amplitude, boolean looping, float period, ActionResponse response) {
		super(0f, amplitude, looping, period, response);
		periodicMode = PeriodicMode.Sine;
	}

	@Override
	public void process() {
		float aux; // angle 
		switch (periodicMode) {
		case Sine:
			aux = ((float)Math.PI * 2) * timePassed / time;
			actualValue = endingValue * (float)Math.sin(aux);
			break;
		case Cossine:
			aux = ((float)Math.PI * 2) * timePassed / time;
			actualValue = endingValue * (float)Math.cos(aux);
			break;
		case Linear:
			final float quarterTime = time / 4;
			
			if (timePassed <= quarterTime ) {
				actualValue = endingValue * timePassed / quarterTime;
			} else if (timePassed <= quarterTime * 2) {
				actualValue = endingValue - endingValue * (timePassed - quarterTime) / quarterTime;
			} else if (timePassed <= quarterTime * 3) {
				actualValue = -endingValue * (timePassed - quarterTime * 2) / quarterTime;
			} else { // (timePassed <= quarterTime * 4)
				actualValue = -endingValue + endingValue * (timePassed - quarterTime * 3) / quarterTime;
			} break;
		}
		
	}

	public PeriodicMode getPeriodicMode() {
		return periodicMode;
	}
	public void setPeriodicMode(PeriodicMode periodicMode) {
		this.periodicMode = periodicMode;
	}

}
